#ifndef AUX_MSGMANAGER_H
#define AUX_MSGMANAGER_H

#include "config.h"

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#include "aux_types.h"
#include "aux_errno.h"
#include "aux_debugmsg.h"
#include "aux_thread.h"

#define QUEUE_NAME_LENGTH_MAX (64)

typedef struct QueueNode{
    void             *pData;        /* the data pointer this node holds */
    u32               nSize;        /* the byte size of data this node holds */
    struct QueueNode *ptNextNode;   /* the next node of this node */
} TQueueNode;

typedef EErrNo (*pfQueueDataDeconstructor)(void *, u32);
typedef EErrNo (*pfQueueDataHandler)(void *, u32, void*);

typedef struct {
#ifdef PLATFORM_LINUX
    pthread_mutex_t  tQueueMutexLock;
#else
    pthread_mutex_t  tQueueMutexLock;
#endif
    TQueueNode               *ptHeadNode; /* the head node of the queue */
    TQueueNode               *ptTailNode; /* the tail node of the queue */
    volatile u32              nNodeCnt;   /* the count this queue head holds */
    u32                       nId;        /* the ID number presents this queue */
    pfQueueDataDeconstructor  pfDestroy;  /* the function destroy  queue data */
    char                      szName[QUEUE_NAME_LENGTH_MAX];
} TQueue;



EErrNo xIPCComponentQueueManagerDeinit();

EErrNo xIPCComponentQueueManagerInit();

EErrNo xIPCQueueCheckAllTrueThenPushBack(TQueue *ptQue,
    pfQueueDataHandler pCb, void *pParameter, void *pData, u32 nSize);

EErrNo xIPCQueueChkValid(const TQueue *ptQue);

EErrNo xIPCQueueClear(TQueue *ptQue, pfQueueDataDeconstructor pCb);

EErrNo   xIPCQueueDeinit(TQueue *ptQueue, pfQueueDataDeconstructor pCb);

EErrNo xIPCQueueDeleteFirstIfCbReturnTrue(TQueue * ptQue,
                        pfQueueDataHandler pCb, void *pParameter);

EErrNo xIPCQueueDeleteHead(TQueue *ptQue, pfQueueDataDeconstructor pCb);

EErrNo xIPCQueueGetFirstIfCbReturnTrue(TQueue *ptQue,
                            pfQueueDataHandler pCb, void *pParameter,
                            void **ppData, u32 *pnSize);

EErrNo xIPCQueueGetHead(TQueue *ptQue, void **ppData, u32 *pnSize);

EErrNo xIPCQueueInit(TQueue *ptQue, const char *pszName,
                           pfQueueDataDeconstructor pDestroy);

EErrNo xIPCQueueNodeCnt(TQueue *ptQue, u32 *pnCnt);

EErrNo xIPCQueuePopHead(TQueue *ptQue, void **ppData, u32 *pnSize);

EErrNo xIPCQueuePushBack(TQueue *ptQue, void *pData, u32 nSize);

EErrNo xIPCQueueTraverse(TQueue *ptQue, pfQueueDataHandler pCb,
                                void *pParameter);

EErrNo xIPCQueueTraverseUntilCbReturnFalse(TQueue *ptQue,
                                                        pfQueueDataHandler pCb,
                                                        void *pParameter);

EErrNo xIPCQueueLock(TQueue *ptQue);

EErrNo xIPCQueueUnlock(TQueue *ptQue);

EErrNo xIPCQueuePushBackPointer(TQueue *ptQue, void *pData);
EErrNo xIPCQueuePopHeadPointer(TQueue *ptQue, void **ppData);
EErrNo xIPCQueueGetHeadPointer(TQueue *ptQue, void **ppData);
EErrNo xIPCQueueDelPointer(TQueue *ptQue, void *pData);


#endif

